package com.sgcc.pda.data.dao;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.sgcc.pda.data.entity.base.SharepreferenceInfo;
import com.sgcc.pda.tools.LogPrintUtils;
import com.sgcc.pda.util.ContentProviderUtil;
import com.sgcc.pda.util.DataConversionUtil;
import com.sgcc.pda.util.PlatConstant;
import com.sgcc.platforminterface.R;


/**
 * 临时数据存储
 *
 * @author:GuangJie-Wang
 * @Date: 2016/4/21
 * @Time: 15:31
 */
public class SharepreferenceUtil {
    /**
     * sp类型：String
     */
    public static final int SP_TYPE_STRING = 0;

    /**
     * sp类型：int
     */
    public static final int SP_TYPE_INT = 1;

    /**
     * sp类型：boolean
     */
    private static final int SP_TYPE_BOOLEAN = 2;

    /**
     * sp类型：float
     */
    public static final int SP_TYPE_FLOAT = 3;

    /**
     * sp类型：long
     */
    public static final int SP_TYPE_LONG = 4;

    /**
     * sp类型：Set<String>
     */
    public static final int SP_TYPE_SET = 5;


    /**********************
     * 数据服务应用包名
     **********************/
    private static final String APP_PACKAGE_DATA = "com.sgcc.pda.data";

    //登录信息
    private static final String LOGININFO = "loginInfo";//保存登录所需信息
    private static final String DEPT_NAME = "DEPT_NAME";//部门名称
    private static final String DEPT_CODE = "DEPT_CODE";//部门编码
    private static final String ORG_NAME = "ORG_NAME";//机构名称
    private static final String ORG_CODE = "ORG_CODE";//机构编码
    private static final String LEADER_FLAG = "LEADER_FLAG";//班组标识，1班组长，0班组成员
    private static final String IS_PHONE = "IS_PHONE";//是否为手机标识
    // 用户人员职责信息
    private static final String OPER_DUTY = "OPER_DUTY";

    // SharedPreferences名称
    /*******************
     * 操作员号和业务员号
     *******************/
    private static final String OPERATE = "operate";
    private static final String OPERATORNO = "operatorNo";//操作员号
    private static final String OPERATORNAME = "operatorName";//操作员号
    private static final String OPERATIONNO = "operationNo";//业务员号
    private static final String DEV_SNNO = "dev_snNo"; //服务终端序列号
    private static final String SIMCARD_SNNO = "simcard_snNo"; //sim卡序列号
    private static final String FAULTNUM = "faultnum"; //消缺达人

    /*******************
     * 上传经纬度配置条件
     *******************/
    private static final String POSITION = "position";
    private static final String MAPAUTHKEY = "mapAuthKey";
    private static final String ISOPEN = "isopen";//是否打开
    private static final String COLLRATE = "collrate";//采集频率
    private static final String LOADRATE = "mapAuthKey";//上传频率
    private static final String ZHENGDUAN = "zhengduan";
    private static final String XIUFU = "xiufu";
    private static final String DEV = "DEV";
    private static final String SIMCARD_SN = "simcard_sn";

    /***********************
     * SIM卡测试参数设置
     ***********************/
    private static final String THINTA = "thinta";
    private static final String COMMUNICATION_IP = "communication_ip";
    private static final String COMMUNICATION_PORT = "communication_port";
    private static final String COMMUNICATION_APN = "communication_apn";

    /**********************
     * 通讯参数设置
     **********************/
    private static final String THINTA_COMM = "thinta_comm";
    private static final String COMM_USBEnable = "comm_usbenable";
    private static final String COMM_IP = "comm_ip";//IP地址
    private static final String COMM_PORT = "comm_port";//端口号
    private static final String COMM_IP_USB = "comm_ip_usb";//USB通信方式IP地址
    private static final String COMM_PORT_USB = "comm_port_usb";//USB通信方式端口号
    private static final String COMM_APNNAME = "comm_apnname";//APN名称
    private static final String COMM_APN = "comm_apn";//APN
    private static final String COMM_USER_NAME = "comm_user_name"; //APN用户名
    private static final String COMM_USER_PSWD = "comm_user_pswd"; //APN密码

    private static final String DATA = "data"; //data定义
    private static final String IM = "im_user_id_flag";
    private static final String IM_USER_ID = "im_user_id";

    private static final String USERFLAG = "userflag";
    private static final String USERID = "userid";
    private static final String USERCODE = "usercode";
    private static final String USER_CODE = "usercode";
    private static final String METER_PWD = "meter_pwd";

    private static final String MAINFLAG = "mainflag";
    private static final String MAIN = "main";

    private static final String LOGINFLAG = "loginflag";
    private static final String LOGIN_FLAG = "login_flag";

    private static final String LOGIN_ = "LOGIN_";
    private static final String LOGIN_M1 = "LOGIN_M1";
    private static final String LOGIN_UID = "LOGIN_UID";
    private static final String LOGIN_MAC = "LOGIN_MAC";

    private static final String SAFE_VERSION = "SAFE_VERSION";//安全单元版本
    private static final String SAFE_VERSION_CODE = "SAFE_VERSION_CODE";//安全单元版本号

    private static final String LOGININFO1 = "loginInfo1";//操作者代码
    private static final String OPERATER = "operater";//操作者代码


    private static final String ESAMNUMINFO = "esamNoInfo";//esam信息
    private static final String ESAMVERSION = "esam";//操作员序列号
    private static final String CESAMNUM = "cesamNo";//操作员序列号
    private static final String CESAMVERSION = "cesamVersion";//cesam对称秘钥版本
    private static final String YESAMNUM = "yesamNo";//业务员序列号
    private static final String YESAMVERSION = "yesamVersion";//yesam对称秘钥版本
    private static final String USER_CODE_BH = "USER_CODE_BH";//闭环的code

    private static final String WORKORDERFLAG = "workorderflag";//下载标记

    private static final String USER_NAME_ = "USER_NAME_";


    /**********************
     * 背夹相关
     **********************/
    private static final String MODE_CHOICE = "MODE_CHOICE";//背夹模式还是掌机模式
    private static final String MODE_CHOICE_CODE = "MODE_CHOICE_CODE";//背夹模式还是掌机模式
    private static final String BLUETOOTH_ADDRESS = "BLUETOOTH_ADDRESS";//蓝牙地址
    private static final String BLUETOOTH_ADDRESS_CODE = "BLUETOOTH_ADDRESS_CODE";//蓝牙地址码

    private static final String LOGIN_AUTH_INFO = "LOGIN_AUTH_INFO";//身份认证信息
    private static final String LOGIN_AUTH_INFO_CODE = "LOGIN_AUTH_INFO_CODE";//身份认证状态码

    public static final String FONT_SCALE_SIZE = "FONT_SCALE_SIZE";

    private static final String BEFORE_VERSION_NAME = "VERSION_NAME";//版本名称
    //中台当前安装版本名称
    private static final String ZHONGTAI_CURRENT_VERSION_CODE = "ZHONGTAI_CURRENT_VERSION_CODE";
    /**
     * 设置电表通讯方式
     */
    private static final String METER_COMM_SETTING = "mode";// editor key
    private static final String METER_COMM_MODE = "mode";// 电表通讯方式
    private static final String METER_485_BAUD = "mode_485_baud";// 485波特率
    private static final String METER_485_DATA_BIT = "mode_485_data_bit";// 485数据位
    private static final String METER_485_CHECK_BIT = "mode_485_check_bit";// 485校验位
    private static final String METER_485_STOP_BIT = "mode_485_stop_bit";// 485停止位

    /**
     * 获取uid
     *
     * @param context
     * @return
     */
    public static String getUid(Context context) {
        String uuid = ContentProviderUtil.getSharepreferenceValue(context, LOGIN_, LOGIN_UID);
        try {
            if (TextUtils.isEmpty(uuid)) {
                LogPrintUtils.d(String.format("获取UUID失败返回登录", PlatConstant.IWebState.login_timeout));
                Intent intent = new Intent();
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent.setComponent(new ComponentName(PlatConstant.IWeb.PACKAGE_NAME, PlatConstant.IWeb.CLASS_NAME)));
                System.exit(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return uuid;
    }

    /**
     * 获取Mac
     *
     * @param context
     * @return
     */
    public static String getMac(Context context) {
        String mac = ContentProviderUtil.getSharepreferenceValue(context, LOGIN_, LOGIN_MAC);
        return TextUtils.isEmpty(mac) ? "12345" : mac;
    }

    /**
     * 获取掌机使用人姓名
     *
     * @param context
     * @return
     */
    public static String getUserNameFromUserList(Context context) {
        String userNameFromUserList = ContentProviderUtil.getSharepreferenceValue(context, OPERATE, USER_NAME_);
        if (TextUtils.isEmpty(userNameFromUserList)) {
            int versionCode = SharepreferenceUtil.getSafeVersion(context);
            String userName = SharepreferenceUtil.getUserName(context);
            if (versionCode == 2 && !TextUtils.isEmpty(userName)) {
                String s = DataConversionUtil.hexStringToChinese(userName.trim());
//                String s = "";
                return s;
            } else {
                return userName;
            }
        } else {
            return userNameFromUserList;
        }
    }

    /**
     * 获取安全单元用户名
     *
     * @param context
     * @return
     */
    public static String getUserName(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, OPERATE, OPERATORNAME);
    }

    /**
     * 设置usb连接
     *
     * @param context
     * @param usbenable
     */
    public static void setCommUsbEnable(Context context, boolean usbenable) {
        String v;
        if (usbenable) {
            v = "1";
        } else {
            v = "0";
        }
        SharepreferenceInfo info = new SharepreferenceInfo();
        info.setEditorKey(THINTA_COMM);
        info.setEditorMode(0);
        info.setKey(COMM_USBEnable);
        info.setValue(v);
        info.setType(SP_TYPE_BOOLEAN);
        ContentProviderUtil.setSharepreference(context, info);
    }


    /**
     * 设置用户名
     *
     * @param context
     * @param userName
     */
    public static void setUserName(Context context, String userName) {
        SharepreferenceInfo info = new SharepreferenceInfo();
        info.setEditorKey(OPERATE);
        info.setEditorMode(0);
        info.setKey(OPERATORNAME);
        info.setValue(userName);
        info.setType(0);
        ContentProviderUtil.setSharepreference(context, info);
    }


    /**
     * 设置操作员编号
     *
     * @param context
     * @param operatorNo
     */
    public static void setOperatorno(Context context, String operatorNo) {
        SharepreferenceInfo info = new SharepreferenceInfo();
        info.setEditorKey(OPERATE);
        info.setEditorMode(0);
        info.setKey(OPERATORNO);
        info.setValue(operatorNo);
        info.setType(0);
        ContentProviderUtil.setSharepreference(context, info);
    }

    /**
     * 设置操作员编号
     *
     * @param context
     * @param operationNo
     */
    public static void setOperationno(Context context, String operationNo) {
        SharepreferenceInfo info = new SharepreferenceInfo();
        info.setEditorKey(OPERATE);
        info.setEditorMode(0);
        info.setKey(OPERATIONNO);
        info.setValue(operationNo);
        info.setType(0);
        ContentProviderUtil.setSharepreference(context, info);
    }

    /**
     * 设置设备编号
     *
     * @param context
     * @param operationNo
     */
    public static void setDevsnno(Context context, String operationNo) {
        SharepreferenceInfo info = new SharepreferenceInfo();
        info.setEditorKey(DEV);
        info.setEditorMode(0);
        info.setKey(DEV_SNNO);
        info.setValue(operationNo);
        info.setType(0);
        ContentProviderUtil.setSharepreference(context, info);
    }

    /**
     * 服务终端序列号
     *
     * @param context
     * @return
     */
    public static String getDevsnno(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, DEV, DEV_SNNO);
    }

    /**
     * 获取usb连接状态
     *
     * @param context
     * @return
     */
    public static boolean getCommUsbEnable(Context context) {
        String state = ContentProviderUtil.getSharepreferenceValue(context, THINTA_COMM, COMM_USBEnable);
        return TextUtils.equals("1", state);
    }

    /**
     * 获取通用usb连接ip
     *
     * @param context
     * @return
     */
    public static String getCommIpUSB(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, THINTA_COMM, COMM_IP_USB);
    }

    /**
     * 获取通用usb连接端口
     *
     * @param context
     * @return
     */
    public static String getCommPortUSB(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, THINTA_COMM, COMM_PORT_USB);
    }

    /**
     * 获取通用设置ip
     *
     * @param context
     * @return
     */
    public static String getCommIp(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, THINTA_COMM, COMM_IP);
    }

    /**
     * 获取通用设置端口
     *
     * @param context
     * @return
     */
    public static String getCommPort(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, THINTA_COMM, COMM_PORT);
    }

    /**
     * 写入sim卡号
     *
     * @param context
     * @param simcard_snNo
     */
    public static void setSimCardSn(Context context, String simcard_snNo) {
        SharepreferenceInfo info = new SharepreferenceInfo();
        info.setEditorKey(SIMCARD_SN);
        info.setEditorMode(0);
        info.setKey(SIMCARD_SNNO);
        info.setValue(simcard_snNo);
        info.setType(0);
        ContentProviderUtil.setSharepreference(context, info);

    }

    /**
     * 获取通用设置端口
     *
     * @param context
     * @return
     */
    public static String getSimCardSn(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, SIMCARD_SN, "MIRROR_TEST");
    }

    /**
     * 获取操作这代码
     *
     * @param context
     * @return
     */
    public static String getOperator(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, LOGININFO1, OPERATER);
    }

    /**
     * 获取操作员编号
     *
     * @param context
     * @return
     */
    public static String getOperatorno(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, OPERATE, OPERATORNO);
    }

    /**
     * 获取业务员编号
     *
     * @param context
     * @return
     */
    public static String getOperationno(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, OPERATE, OPERATIONNO);
    }

    /**
     * @param context
     * @return
     */
    public static String getUserCode(Context context) {
        boolean toUpper = Boolean.parseBoolean(context.getString(R.string.lib_aidl_platform_upper_user_code));
        if (toUpper) {
            return ContentProviderUtil.getSharepreferenceValue(context, USERCODE, USER_CODE).toUpperCase();
        }
        return ContentProviderUtil.getSharepreferenceValue(context, USERCODE, USER_CODE);
    }

    /**
     * 获取账号 usercode ，此信息是与中台一致
     *
     * @param context
     * @return
     */
    public static String getUserCodeForOriginalData(Context context) {
        return getValueFromContentProvider(context, USERCODE, USER_CODE, "");
    }

    /**
     * @param context
     * @return
     */
    public static String getOptCode(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, OPERATE, OPERATIONNO);
    }

    /**
     * 获取单位名称
     *
     * @param context
     * @return
     */
    public static String getDeptName(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, LOGININFO, DEPT_NAME);
    }

    /**
     * 获取单位编码
     *
     * @param context
     * @return
     */
    public static String getDeptCode(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, LOGININFO, DEPT_CODE);
    }

    /**
     * 获取机构名称
     *
     * @param context
     * @return
     */
    public static String getOrgName(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, LOGININFO, ORG_NAME);
    }

    /**
     * 获取机构编码
     *
     * @param context
     * @return
     */
    public static String getOrgCode(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, LOGININFO, ORG_CODE);
    }

    /**
     * 获取班组标识
     *
     * @param context
     * @return
     */
    public static String getLeaderFlag(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, LOGININFO, LEADER_FLAG);
    }

    /**
     * @param context
     * @return
     */
    public static String getM1(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, LOGIN_, LOGIN_M1);
    }


    /**
     * 获取安全单元版本
     *
     * @param context
     * @return 0未设置  1安全单元1.0  2安全单元2.0
     */
    public static int getSafeVersion(Context context) {
        if (!TextUtils.isEmpty(ContentProviderUtil.getSharepreferenceValue(context, SAFE_VERSION, SAFE_VERSION_CODE))) {
            try {
                return Integer.parseInt(ContentProviderUtil.getSharepreferenceValue(context, SAFE_VERSION, SAFE_VERSION_CODE));
            } catch (NumberFormatException e) {
                e.printStackTrace();
                return 0;
            }
        } else {
            return 0;
        }
    }

    /**
     * Y-EESAM密钥版本
     *
     * @param context
     * @return
     */
    public static String getYEsamVersion(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, ESAMVERSION, YESAMVERSION);
    }


    /**
     * C-EESAM密钥版本
     *
     * @param context
     * @return
     */
    public static String getCEsamVersion(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, ESAMVERSION, CESAMVERSION);
    }

    /**
     * C-EESAM序列号
     *
     * @param context
     * @return
     */
    public static String getCEsamNum(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, ESAMNUMINFO, CESAMNUM);
    }

    /**
     * Y-EESAM序列号
     *
     * @param context
     * @return
     */
    public static String getYEsamNum(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, ESAMNUMINFO, YESAMNUM);
    }

    /**
     * 获取闭环的code
     */
    public static String getBHUserCode(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, USERCODE, USER_CODE_BH);
    }

    /**
     * 获取平台设置的电表的密码
     */
    public static String getMeterPwd(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, THINTA_COMM, METER_PWD);
    }


    /**
     * @param context
     * @return 读取平台转加密标志
     */
    public static String getChangeEncrypt(Context context) {
        if (isBluetoothMode(context)) {
            return ContentProviderUtil.getSharepreferenceValue(context, "SESSIONKEYOFFLINE_TIME", "SESSIONKEYOFFLINE_TIME" + getBluetoothAddress(context));
        } else {
            return ContentProviderUtil.getSharepreferenceValue(context, "SESSIONKEYOFFLINE_TIME", "SESSIONKEYOFFLINE_TIME");
        }
    }

    /**
     * @param context
     * @param num     0：增量下载   1：全量下载
     */
    public static void setDownFlag(Context context, String num) {
        SharedPreferences.Editor editor = context.getSharedPreferences(
                WORKORDERFLAG, 0).edit();
        editor.putString("Flag", num);
        editor.commit();
    }

    //默认是全量1
    public static String getDownFlag(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(
                WORKORDERFLAG, 0);
        String result = preferences.getString("Flag", "1");
        if (SharepreferenceUtil.getSafeVersion(context) == 1)
            return result;
        else {
            String romte = SharepreferenceUtil.getChangeEncrypt(context);
            if (TextUtils.isEmpty(romte)) {
                return "1";
            }
            if (TextUtils.equals(romte, SharepreferenceUtil.getChangeEncrypt(context))) {
                return result;
            } else {
                SharepreferenceUtil.setChangeEncrypt(context, romte);
                return "1";
            }
        }
    }

    /**
     * 保存本地转加密标志
     *
     * @param context
     * @param
     */
    private static void setChangeEncrypt(Context context, String key) {
        SharepreferenceInfo info = new SharepreferenceInfo();
        info.setEditorKey("SESSIONKEYOFFLINE_TIME");
        info.setEditorMode(0);
        info.setKey("SESSIONKEYOFFLINE_TIME");
        info.setValue(key);
        info.setType(0);
        ContentProviderUtil.setSharepreference(context, info);
    }

    /**
     * 是否是背夹模式
     *
     * @param context
     * @return
     */
    public static boolean isBluetoothMode(Context context) {
        String boolStr = ContentProviderUtil.getSharepreferenceValue(context, MODE_CHOICE, MODE_CHOICE_CODE);
        return !TextUtils.isEmpty(boolStr) && Boolean.parseBoolean(boolStr);
    }

    /**
     * 是否是手机模式
     *
     * @param context 上下文
     * @return
     */
    public static boolean isPhoneMode(Context context) {
        String boolStr = ContentProviderUtil.getSharepreferenceValue(context, LOGININFO, IS_PHONE);
        return !TextUtils.isEmpty(boolStr) && "1".equals(boolStr);
    }

    /**
     * 获取背夹模式下背夹蓝牙的mac地址
     *
     * @param context
     * @return
     */
    public static String getBluetoothAddress(Context context) {
        String blueAddress = ContentProviderUtil.getSharepreferenceValue(context, BLUETOOTH_ADDRESS, BLUETOOTH_ADDRESS_CODE);
        return TextUtils.isEmpty(blueAddress) ? "" : blueAddress;
    }

    /**
     * 获取身份认证成功还是失败
     *
     * @param context 上下文
     * @return 状态
     */
    public static String getLoginAuthFlag(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, LOGIN_AUTH_INFO, LOGIN_AUTH_INFO_CODE + getBluetoothAddress(context));
    }

    /**
     * 设置身份认证成功还是失败
     *
     * @param context 上下文
     * @param success 状态
     */
    public static void setLoginAuthFlag(Context context, String success) {
        SharepreferenceInfo info = new SharepreferenceInfo();
        info.setEditorKey(LOGIN_AUTH_INFO);
        info.setEditorMode(0);
        info.setKey(LOGIN_AUTH_INFO_CODE + getBluetoothAddress(context));
        info.setValue(success);
        info.setType(0);
        ContentProviderUtil.setSharepreference(context, info);
    }

    private static final String COMM_PARAM = "COMM_PARAM";// 系统通用参数

    private static final String ONE_KEY_PACKAGE_STATE = "IS_BINDING_CHECK_KEY";// 一键执行功能码：0：关闭； 1：开启

    public static void setOneKeyPackageState(Context context, String oneKeyPackageState) {
        SharepreferenceInfo info = new SharepreferenceInfo();
        info.setEditorKey(COMM_PARAM);
        info.setEditorMode(0);
        info.setKey(ONE_KEY_PACKAGE_STATE);
        info.setValue(oneKeyPackageState);
        info.setType(0);
        ContentProviderUtil.setSharepreference(context, info);
    }

    public static String getOneKeyPackageState(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, COMM_PARAM, ONE_KEY_PACKAGE_STATE);
    }

    /**
     * 设置字体缩放比例
     *
     * @param context
     * @param fontScaleSize
     */
    public static void setFontScaleSize(Context context, float fontScaleSize) {
        SharepreferenceInfo info = new SharepreferenceInfo();
        info.setEditorKey(COMM_PARAM);
        info.setEditorMode(0);
        info.setKey(FONT_SCALE_SIZE + "_" + getUserCodeForOriginalData(context));
        info.setValue(String.valueOf(fontScaleSize));
        info.setType(0);
        ContentProviderUtil.setSharepreference(context, info);
    }

    /**
     * 获取字体缩放比例
     *
     * @param context
     * @return 默认返回不缩放
     */
    public static float getFontScaleSize(Context context) {
        return getFontScaleSize(context, 1f);
    }

    /**
     * 获取字体缩放比例
     *
     * @param context
     * @param defaultValue 默认值
     * @return
     */
    public static float getFontScaleSize(Context context, float defaultValue) {
        String fontScaleSize = ContentProviderUtil.getSharepreferenceValue(context, COMM_PARAM,
                FONT_SCALE_SIZE + "_" + getUserCodeForOriginalData(context));
        if (TextUtils.isEmpty(fontScaleSize)) {
            return defaultValue;
        }
        float fontScale = defaultValue;
        try {
            fontScale = Float.parseFloat(fontScaleSize);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return fontScale;
    }

    /**
     * 获取中台当前版本号
     *
     * @param context
     * @return
     */
    public static String getAppCurrentVersionCode(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, BEFORE_VERSION_NAME, ZHONGTAI_CURRENT_VERSION_CODE);
    }

    /**
     * 从中台读取共享值
     *
     * @param context
     * @param spName
     * @param key
     * @return
     */
    public static String getValueFromContentProvider(Context context, String spName, String key) {
        return ContentProviderUtil.getSharepreferenceValue(context, spName, key);
    }

    /**
     * 从中台读取共享值
     *
     * @param context
     * @param spName
     * @param key
     * @return
     */
    public static String getValueFromContentProvider(Context context, String spName, String key, String defaultValue) {
        String value = getValueFromContentProvider(context, spName, key);
        return TextUtils.isEmpty(value) ? defaultValue : value;
    }

    /**
     * 设置中台读取共享值
     *
     * @param context
     * @param spName
     * @param key
     * @return
     */
    public static void setValueToContentProvider(Context context, String spName, String key, String value) {
        SharepreferenceInfo info = new SharepreferenceInfo();
        info.setEditorKey(spName);
        info.setEditorMode(0);
        info.setKey(key);
        info.setValue(value);
        info.setType(0);
        ContentProviderUtil.setSharepreference(context, info);
    }

    /**
     * 设置电表通讯方式
     *
     * @param context
     * @param meterCommMode "0" = "默认"
     *                      "1" = "485串口"
     *                      "2" = "红外"
     *                      "3" = "激光红外"
     */
    public static void setMeterCommMode(Context context, String meterCommMode) {
        SharepreferenceInfo sharepreferenceInfo = new SharepreferenceInfo();
        sharepreferenceInfo.setKey(METER_COMM_MODE);
        sharepreferenceInfo.setValue(meterCommMode);
        sharepreferenceInfo.setEditorKey(METER_COMM_SETTING);
        sharepreferenceInfo.setEditorMode(Context.MODE_PRIVATE);
        sharepreferenceInfo.setType(0);
        ContentProviderUtil.setSharepreference(context, sharepreferenceInfo);
    }

    /**
     * 获取保存的电表通讯方式
     *
     * @param context
     * @return "0" = "默认"
     * "1" = "485串口"
     * "2" = "红外"
     * "3" = "激光红外"
     */
    public static String getMeterCommMode(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, METER_COMM_SETTING, METER_COMM_MODE);
    }

    /**
     * 设置电表通讯方式-485串口波特率
     *
     * @param context
     * @param baud    波特率范围为1200、2400、4800、9600、14400、19200、38400
     */
    public static void setMeter485Baud(Context context, String baud) {
        SharepreferenceInfo sharepreferenceInfo = new SharepreferenceInfo();
        sharepreferenceInfo.setKey(METER_485_BAUD);
        sharepreferenceInfo.setValue(baud);
        sharepreferenceInfo.setEditorKey(METER_COMM_SETTING);
        sharepreferenceInfo.setEditorMode(Context.MODE_PRIVATE);
        sharepreferenceInfo.setType(0);
        ContentProviderUtil.setSharepreference(context, sharepreferenceInfo);
    }

    /**
     * 获取电表通讯方式-485串口波特率
     *
     * @param context
     * @return 波特率范围为1200、2400、4800、9600、14400、19200、38400
     */
    public static String getMeter485Baud(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, METER_COMM_SETTING, METER_485_BAUD);
    }

    /**
     * 设置电表通讯方式-485串口数据位
     *
     * @param context
     * @param bit     数据位范围：5、6、7、8
     */
    public static void setMeter485DataBit(Context context, String bit) {
        SharepreferenceInfo sharepreferenceInfo = new SharepreferenceInfo();
        sharepreferenceInfo.setKey(METER_485_DATA_BIT);
        sharepreferenceInfo.setValue(bit);
        sharepreferenceInfo.setEditorKey(METER_COMM_SETTING);
        sharepreferenceInfo.setEditorMode(Context.MODE_PRIVATE);
        sharepreferenceInfo.setType(0);
        ContentProviderUtil.setSharepreference(context, sharepreferenceInfo);
    }

    /**
     * 获取电表通讯方式-485串口数据位
     *
     * @param context
     * @return 数据位范围：5、6、7、8
     */
    public static String getMeter485DataBit(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, METER_COMM_SETTING, METER_485_DATA_BIT);
    }

    /**
     * 设置电表通讯方式-485串口校验位
     *
     * @param context
     * @param bit     校验位范围：无(0)、奇(1)、偶(2)
     */
    public static void setMeter485CheckBit(Context context, String bit) {
        SharepreferenceInfo sharepreferenceInfo = new SharepreferenceInfo();
        sharepreferenceInfo.setKey(METER_485_CHECK_BIT);
        sharepreferenceInfo.setValue(bit);
        sharepreferenceInfo.setEditorKey(METER_COMM_SETTING);
        sharepreferenceInfo.setEditorMode(Context.MODE_PRIVATE);
        sharepreferenceInfo.setType(0);
        ContentProviderUtil.setSharepreference(context, sharepreferenceInfo);
    }

    /**
     * 获取电表通讯方式-485串口校验位
     *
     * @param context
     * @return 校验位范围：无(0)、奇(1)、偶(2)
     */
    public static String getMeter485CheckBit(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, METER_COMM_SETTING, METER_485_CHECK_BIT);
    }

    /**
     * 设置电表通讯方式-485串口停止位
     *
     * @param context
     * @param bit     停止位范围：0、1、1.5、2
     */
    public static void setMeter485StopBit(Context context, String bit) {
        SharepreferenceInfo sharepreferenceInfo = new SharepreferenceInfo();
        sharepreferenceInfo.setKey(METER_485_STOP_BIT);
        sharepreferenceInfo.setType(0);
        ContentProviderUtil.setSharepreference(context, sharepreferenceInfo);
        sharepreferenceInfo.setValue(bit);
        sharepreferenceInfo.setEditorKey(METER_COMM_SETTING);
        sharepreferenceInfo.setEditorMode(Context.MODE_PRIVATE);
    }

    /**
     * 获取电表通讯方式-485串口停止位
     *
     * @param context
     * @return 停止位范围：0、1、1.5、2
     */
    public static String getMeter485StopBit(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, METER_COMM_SETTING, METER_485_STOP_BIT);
    }

    /**
     * 保存用户人员职责信息
     */
    public static void setOperDuty(Context context, String operDuty) {
        SharepreferenceInfo info = new SharepreferenceInfo();
        info.setEditorKey(LOGININFO);
        info.setEditorMode(0);
        info.setKey(OPER_DUTY);
        info.setValue(operDuty != null ? operDuty : "");
        info.setType(0);
        ContentProviderUtil.setSharepreference(context, info);
    }

    /**
     * 获取用户人员职责信息
     */
    public static String getOperDuty(Context context) {
        return ContentProviderUtil.getSharepreferenceValue(context, LOGININFO, OPER_DUTY);
    }
}
